package com.casic.ops.configs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.casic.ops.dao.SonarqubeInfoDAO;
import com.casic.ops.entity.domain.SonarqubeDashboardBean;
import com.casic.ops.utils.OpsAssert;
import com.geewaza.sonarqube.webapi.SonarClient;
import com.geewaza.sonarqube.webapi.model.component.Component;
import com.geewaza.sonarqube.webapi.model.component.Components;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @ClassName ops
 * @Description soanrqube 的配置文件
 * @Author mianbin
 * @Date 2022/6/28 19:46
 * @Version 1.0
 **/
@Data
@Slf4j
@Service
@EnableScheduling
@ConfigurationProperties(prefix = "sonarqubeprops")
public class SonarqubeProps {

    private String url;
    private String login;
    private String adminuser;
    private String adminpasswd;
    private String token;

    //
    // 这里暂时不飞异常
    //
    @Bean
    public SonarClient sonarClient() {
        try {
            return new SonarClient(new URI(url), token);
        } catch (URISyntaxException e) {
            log.info("sonarqube 的采用token密钥执行异常");
        }
        try {
            return new SonarClient(new URI(url), adminuser, adminpasswd);
        } catch (URISyntaxException e) {
            log.info("sonarqube 的采用密码登陆执行异常");
        }
        return null;
    }

    @Resource
    private SonarqubeInfoDAO sonarqubeInfoDAO;

    //
    // 代码分析
    //
    @Scheduled(initialDelay = 10000, fixedDelay = 10000)
    public void codeStatistics() {
        SonarClient sonarClient = ApplicationContextUtil.getBean(SonarClient.class);
        Optional.ofNullable(sonarClient).ifPresent(var -> {
            Components execute = sonarClient.getProjectsClient().searchProject().execute();
            for (Component component : execute.getComponents()) {
                String key = component.getKey();
                int codeSmell_total = sonarClient.getIssuesClient().searchIssues().componentKeys(key).types("CODE_SMELL").execute().getPaging().getTotal();
                int bug_total = sonarClient.getIssuesClient().searchIssues().componentKeys(key).types("BUG").execute().getPaging().getTotal();
                int vulnerability_total = sonarClient.getIssuesClient().searchIssues().componentKeys(key).types("VULNERABILITY").execute().getPaging().getTotal();
                SonarqubeDashboardBean sonar = SonarqubeDashboardBean.builder()
                        .projectKey(key)
                        .codeSmellTotal(codeSmell_total)
                        .bugTotal(bug_total)
                        .vulnerabilitieTotal(vulnerability_total).build();
                /* 好蠢的mybatis,后面换*/
                LambdaQueryWrapper<SonarqubeDashboardBean> wrappers = new LambdaQueryWrapper<SonarqubeDashboardBean>().eq(SonarqubeDashboardBean::getProjectKey, key);
                SonarqubeDashboardBean sonarqubeDashboardBean = sonarqubeInfoDAO.
                        selectOne(wrappers);
                if (Objects.nonNull(sonarqubeDashboardBean)) {
                    sonarqubeInfoDAO.update(sonar, wrappers);
                } else {
                    sonarqubeInfoDAO.insert(sonar);
                }
            }
        });
    }

}
